/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tolls;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.JOptionPane;

/**
 *
 * @author Aggelos
 */
public class PaymentForm extends javax.swing.JFrame {
    EntityManager em;
    Query paymentQuery;
    List paymentList;
    String strCardId;
    String strRegNum;
    String strBalance;
    String strToday;
    String sqlToday;
    Double payment;
    Double balance;
    Double total;
    String strTotal;
    /**
     * Creates new form PaymentForm
     */
    public PaymentForm(EntityManager em) {
        initComponents();
    }
    //Constructor για το πέρασμα των στοιχείων από την CollectTollForm στην Payment
    public PaymentForm(EntityManager em,String strCardId,String strRegNum,String strBalance){
      this.em=em;
      initComponents();
      this.strRegNum=strRegNum;
      this.strCardId=strCardId;
      this.strBalance=strBalance;
      cardIdField.setText(strCardId); 
      regNumField.setText(strRegNum);
      balanceField.setText(strBalance);
       
      
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cardIdLabel = new javax.swing.JLabel();
        cardIdField = new javax.swing.JTextField();
        regNumLabel = new javax.swing.JLabel();
        regNumField = new javax.swing.JTextField();
        paymentButton = new javax.swing.JButton();
        balanceLabel = new javax.swing.JLabel();
        balanceField = new javax.swing.JTextField();
        paymentLabel = new javax.swing.JLabel();
        paymentField = new javax.swing.JTextField();
        newBalanceLabel = new javax.swing.JLabel();
        newBalanceField = new javax.swing.JTextField();
        checkButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Πληρωμή Κάρτας");

        cardIdLabel.setText("Αρ. Κάρτας : ");

        cardIdField.setEditable(false);

        regNumLabel.setText("Αρ. Πινακίδας : ");

        regNumField.setEditable(false);

        paymentButton.setText("Πληρωμή");
        paymentButton.setEnabled(false);
        paymentButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paymentButtonActionPerformed(evt);
            }
        });

        balanceLabel.setText("Υπόλοιπο : ");

        balanceField.setEditable(false);

        paymentLabel.setText("Ποσό Πληρωμής :  ");

        paymentField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paymentFieldActionPerformed(evt);
            }
        });

        newBalanceLabel.setText("Νέο Υπόλοιπο : ");

        checkButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        checkButton.setText("Έλεγχος Πληρωμής");
        checkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        cancelButton.setText("Άκυρο");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(paymentButton)
                        .addGap(86, 86, 86)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newBalanceLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newBalanceField, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(balanceLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(balanceField, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cardIdLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cardIdField, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(paymentLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(paymentField, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(regNumLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(regNumField, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(checkButton))))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cardIdLabel)
                    .addComponent(cardIdField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(regNumLabel)
                    .addComponent(regNumField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(balanceLabel)
                    .addComponent(balanceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(paymentLabel)
                    .addComponent(paymentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newBalanceLabel)
                    .addComponent(newBalanceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(paymentButton)
                    .addComponent(cancelButton))
                .addContainerGap(68, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkButtonActionPerformed
        // TODO add your handling code here:
       payment=Double.parseDouble(paymentField.getText());
       if (payment<10){ //Αν το ποσό πληρωμής-φόρτισης είναι μικρότερο των 10 ευρώ
           JOptionPane.showMessageDialog(this,"Το ποσό που έχετε εισάγει δεν είναι αποδεκτό. Παρακαλώ εισάγετε ποσό μεγαλύτερο των 10 ευρώ.");
       }
       else{    //Αν το ποσό πληρωμής-φόρτισης είναι μικρότερο των 10 ευρώ
           balance=Double.parseDouble(strBalance);
           total=payment+balance;  //Υπολογισμός νέου υπολοίπου που θα προκύψει μετα την φόρτιση
           strTotal=String.valueOf(total); //Μετατροπή από Double σε String
           newBalanceField.setText(strTotal); //Εισαγωγή στο Textfield, του νέου υπολοίπου που θα προκύψει μετα την φόρτιση
           paymentButton.setEnabled(true);  //Ενεργοποίηση κουμπιού Πληρωμής
       }
    }//GEN-LAST:event_checkButtonActionPerformed

    private void paymentFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_paymentFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_paymentFieldActionPerformed

    private void paymentButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_paymentButtonActionPerformed
        // TODO add your handling code here:
        if(!(em.getTransaction().isActive())){
        em.getTransaction().begin();
    }
     int cardId=Integer.parseInt(strCardId);  
        Date date = new Date();    //Παίρνουμε την σημερινή ημερομηνία
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());  //τη μετατρέπουμε σε sqlDate για να την εισάγουμε στη βάση

        //Εισαγωγή των στοιχείων της Πληρωμής στη βάση
       em.createNativeQuery("insert into payment(card_id,payment_timestamp,old_balance) values("+cardId+",'"+sqlDate+"',"+total+")").executeUpdate();
       //Ενημέρωση της κάρτας στη βάση με το νέο υπόλοιπο που προκύπτει μετά την φόρτιση
       em.createNativeQuery("update card c set c.balance= " + total + " where id = " + cardId).executeUpdate();
       em.getTransaction().commit();
        JOptionPane.showMessageDialog(this,"Η Πληρωμή ολοκληρώθηκε Επιτυχώς!");
        dispose();
        
        
    }//GEN-LAST:event_paymentButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
         dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField balanceField;
    private javax.swing.JLabel balanceLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField cardIdField;
    private javax.swing.JLabel cardIdLabel;
    private javax.swing.JButton checkButton;
    private javax.swing.JTextField newBalanceField;
    private javax.swing.JLabel newBalanceLabel;
    private javax.swing.JButton paymentButton;
    private javax.swing.JTextField paymentField;
    private javax.swing.JLabel paymentLabel;
    private javax.swing.JTextField regNumField;
    private javax.swing.JLabel regNumLabel;
    // End of variables declaration//GEN-END:variables
}
